"""
主要用来验证前端提交的数据是否符合要求
"""
import wtforms
from wtforms.validators import Email, Length, EqualTo, Regexp
from models import UserModel


# 用户基类的属性检测
class RegisterForm(wtforms.Form):
    userName = wtforms.StringField(validators=[Length(min=1,max=10,message="用户名长度错误")])
    email = wtforms.StringField(validators=[Email(message="邮箱格式错误！")])
    phone = wtforms.StringField(validators=[Length(min=11,max=11),Regexp(regex="^1[3456789]\d{9}")])
    passWord = wtforms.StringField(validators=[Length(min=1, max=4)]) # Regexp(regex="^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*_-])[a-zA-Z\d!@#$%^&*_-]{8,18}")

    """
    也可以自定义验证：例如邮箱是否已经被注册、验证码是否正确等等
    """

    def validate_mail(self, field):
        # 验证邮箱是否被注册过
        email = field.data
        user = UserModel.query.filter_by(email=email).first()
        if user:
            raise wtforms.ValidationError(message="该邮箱已经被注册！")

    def validate_phone(self,field):
        # 验证手机号是否被注册过
        phone =field.data
        user = UserModel.query.filter_by(phone=phone).first()
        if user:
            raise wtforms.ValidationError(message="该手机号已经被注册！")

    def validate_userName(self, field):
        # 验证用户名是否被注册过
        userName=field.data
        user=UserModel.query.filter_by(userName=userName).first()
        if user:
            raise wtforms.ValidationError(message="该用户名已被注册")


class LoginForm(wtforms.Form):
    # todo:和注册一样，添加登录检测条件
    pass
